package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ProductDao;
import dao.Factoty.DaoFactory;
import domain.Product;
import service.Factory.ServiceFactory;

/**
 * Servlet implementation class AllProductServlet
 */
@WebServlet("/AllProductServlet")
public class AllProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AllProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Product> products;
		int page = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
		int limit = 8;
		
		try {
			products = ServiceFactory.getProductServiceInstance().findAllProducts(page, limit);

			request.setAttribute("products", products);
			request.setAttribute("_page", page);
			request.setAttribute("hasPrev", page > 1);
			request.setAttribute("hasNext", products.size() == limit);
			request.getRequestDispatcher("/client/ProductList.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
